package com.zhk.entry.test;

import java.lang.reflect.Proxy;
import java.util.Observable;
import java.util.Observer;

/**
 * @Description 观察者
 * @Author zhanghk
 * @Date 2021/4/6 17:14
 */
public class Person implements Observer {

    private Observable observable;

    public Person(){}
    public void setPerson(Observable observable) {

        this.observable = observable;
        observable.addObserver((Observer) Proxy.newProxyInstance(Person.class.getClassLoader(),Person.class.getInterfaces(),new Nanny()));
    }

    @Override
    public void update(Observable o, Object arg) {
        String msg = (String) arg;
        if ("commit".equals(msg)) {
            System.out.println("对事务进行提交");
        }
        if ("rollback".equals(msg)) {
            System.out.println("对事务进行回滚");
        }
    }
}
